package com.tckdq.tcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tckdq.tcc.constants.RedisConstant;
import com.tckdq.tcc.domain.CourseType;
import com.tckdq.tcc.dto.CrumbsDTO;
import com.tckdq.tcc.mapper.CourseTypeMapper;
import com.tckdq.tcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-08-16
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private RedisTemplate<Object,Object> RedisTemplate;


    @Autowired
    private CourseTypeMapper courseTypeMapper;

    @Override
    //添加缓存
    @Cacheable(cacheNames = RedisConstant.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public List<CourseType> selTree() {
        /*List<CourseType> listTrees = (List<CourseType>)RedisTemplate.opsForValue().get(RedisConstant.KEY_COURSE_TYPE_TREE);
        if (!StringUtils.isEmpty(listTrees)){
            return listTrees;
        }*/
        List<CourseType> courseTypesTree = treeData();
        //RedisTemplate.opsForValue().set(RedisConstant.KEY_COURSE_TYPE_TREE,courseTypesTree);
        return courseTypesTree;

    }
    //面包屑
    @Override
    public List<CrumbsDTO> getCrumb(Long courseTypeId) {
        CourseType courseType = selectById(courseTypeId);
        String path = courseType.getPath();
        String[] strPath = path.split("\\.");
        List<CrumbsDTO> crumbsDTOS = new ArrayList<>();
        for (String id : strPath) {
            CrumbsDTO crumbsDTO = new CrumbsDTO();
            CourseType ownerProductType = selectById(id);
            EntityWrapper<CourseType> wrapper = new EntityWrapper<>();
            wrapper.eq("pid",ownerProductType.getPid()).ne("id",ownerProductType.getId());
            //兄弟分类
            List<CourseType> courseTypes = selectList(wrapper);
            crumbsDTO.setOwnerProductType(ownerProductType);
            crumbsDTO.setOtherProductTypes(courseTypes);
            crumbsDTOS.add(crumbsDTO);
        }
        return crumbsDTOS;
    }

    public List<CourseType> treeData(){
        List<CourseType> listTree = new ArrayList<>();

        List<CourseType> courseTypes = selectList(null);

        HashMap<Long, CourseType> map = new HashMap<>();

        for (CourseType courseType : courseTypes) {

            map.put(courseType.getId(),courseType);

            Long pid = courseType.getPid();
            if (pid==null | pid==0){
                listTree.add(courseType);
            }else {
                CourseType parentCourseType = map.get(pid);
                parentCourseType.getChildren().add(courseType);
            }
        }

        return listTree;
    }

    @Override
    @CacheEvict(cacheNames = RedisConstant.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public boolean deleteById(Serializable id) {
        boolean deleteById = super.deleteById(id);
       // if (deleteById)RedisTemplate.delete(RedisConstant.COURSE_TREE);

        return deleteById;
    }

    @Override
    @CacheEvict(cacheNames = RedisConstant.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public boolean updateBatchById(List<CourseType> entityList) {
        boolean updateBatchById = super.updateBatchById(entityList);
        //if (updateBatchById)RedisTemplate.delete(RedisConstant.COURSE_TREE);
        return updateBatchById;
    }

    @Override
    @CacheEvict(cacheNames = RedisConstant.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public boolean insert(CourseType entity) {
        boolean insert = super.insert(entity);
       // if (insert)RedisTemplate.delete(RedisConstant.COURSE_TREE);
        return insert;
    }
}
